package com.zzedu.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.domain.entity.SysDictType;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.core.service.ISysDictTypeService;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@permission.hasAnyPermissions('system:dict:list')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody SysDictType dictType) {
        IPage<SysDictType> list = dictTypeService.selectDictTypeList(dictType.getPageDTO(), dictType);
        return getDataTable(list);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PreAuthorize("@permission.hasAnyPermissions('system:dict:export')")
    @RequestMapping("/export")
    public void export(HttpServletResponse response, SysDictType dictType) {
        IPage<SysDictType> list = dictTypeService.selectDictTypeList(new Page<>(1l, -1l), dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list.getRecords(), "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:dict:query')")
    @RequestMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable Long dictId) {
        return success(dictTypeService.getById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add")
    public AjaxResult add(@Validated @RequestBody SysDictType dict) {
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit")
    public AjaxResult edit(@Validated @RequestBody SysDictType dict) {
        return toAjax(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(Arrays.stream(dictIds).collect(Collectors.toList()));
        return success();
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @RequestMapping("/refreshCache")
    public AjaxResult refreshCache() {
        dictTypeService.resetDictCache();
        return success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return success(dictTypes);
    }
}
